/**
 * Licensed Property to China UnionPay Co., Ltd.
 * <p>
 * (C) Copyright of China UnionPay Co., Ltd. 2010
 * All Rights Reserved.
 */
package com.sunyard.utils.alipayutil;

import javax.net.ssl.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.cert.X509Certificate;

/**
 * @author mazhao
 * @ClassName BaseHttpSSLSocketFactory
 * @Package com.sunyard.util.alipayutil
 * @Description 忽略验证ssl证书
 * 声明：以下代码只是为了方便接入方测试而提供的样例代码，商户可以根据自己需要，按照技术文档编写。该代码仅供参考，不提供编码，性能，规范性等方面的保障
 * @Date 2018/11/29 10:49
 * @Version V1.0
 */
public class BaseHttpSSLSocketFactory extends SSLSocketFactory {
    /**
     * @return javax.net.ssl.SSLContext
     * @Method: getSSLContext
     * @Decription: TODO
     * @author mazhao
     * @Date 2018/11/28 16:31
     * @Param []
     * @throw
     */
    private SSLContext getSSLContext() {
        return createEasySSLContext();
    }

    /**
     * @return java.net.Socket
     * @Method: createSocket
     * @Decription: TODO
     * @author mazhao
     * @Date 2018/11/28 16:31
     * @Param [arg0, arg1, arg2, arg3]
     * @throw
     */
    @Override
    public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2,
                               int arg3) throws IOException {
        return getSSLContext().getSocketFactory().createSocket(arg0, arg1,
                arg2, arg3);
    }

    /**
     * @return java.net.Socket
     * @Method: createSocket
     * @Decription: TODO
     * @author mazhao
     * @Date 2018/11/28 16:31
     * @Param [arg0, arg1, arg2, arg3]
     * @throw
     */
    @Override
    public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3)
            throws IOException, UnknownHostException {
        return getSSLContext().getSocketFactory().createSocket(arg0, arg1,
                arg2, arg3);
    }

    /**
     * @return java.net.Socket
     * @Method: createSocket
     * @Decription: TODO
     * @author mazhao
     * @Date 2018/11/28 16:31
     * @Param [arg0, arg1]
     * @throw
     */
    @Override
    public Socket createSocket(InetAddress arg0, int arg1) throws IOException {
        return getSSLContext().getSocketFactory().createSocket(arg0, arg1);
    }

    /**
     * @return java.net.Socket
     * @Method: createSocket
     * @Decription: TODO
     * @author mazhao
     * @Date 2018/11/28 16:31
     * @Param [arg0, arg1]
     * @throw
     */
    @Override
    public Socket createSocket(String arg0, int arg1) throws IOException,
            UnknownHostException {
        return getSSLContext().getSocketFactory().createSocket(arg0, arg1);
    }

    /**
     * @return java.lang.String[]
     * @Method: getSupportedCipherSuites
     * @Decription: TODO
     * @author mazhao
     * @Date 2018/11/28 16:31
     * @Param []
     * @throw
     */
    @Override
    public String[] getSupportedCipherSuites() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @return java.lang.String[]
     * @Method: getDefaultCipherSuites
     * @Decription: TODO
     * @author mazhao
     * @Date 2018/11/28 16:31
     * @Param []
     * @throw
     */
    @Override
    public String[] getDefaultCipherSuites() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @return java.net.Socket
     * @Method: createSocket
     * @Decription: TODO
     * @author mazhao
     * @Date 2018/11/28 16:31
     * @Param [arg0, arg1, arg2, arg3]
     * @throw
     */
    @Override
    public Socket createSocket(Socket arg0, String arg1, int arg2, boolean arg3)
            throws IOException {
        return getSSLContext().getSocketFactory().createSocket(arg0, arg1,
                arg2, arg3);
    }

    /**
     * @return javax.net.ssl.SSLContext
     * @Method: createEasySSLContext
     * @Decription: TODO
     * @author mazhao
     * @Date 2018/11/28 16:31
     * @Param []
     * @throw
     */
    private SSLContext createEasySSLContext() {
        try {
            SSLContext context = SSLContext.getInstance("SSL");
            context.init(null,
                    new TrustManager[]{MyX509TrustManager.manger}, null);
            return context;
        } catch (Exception e) {
            AliPayUtil.getLogger().error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * @author mazhao
     * @Method:
     * @Decription: TODO
     * @Date 2018/11/28 16:32
     * @Param
     * @return
     * @throw
     */
    public static class MyX509TrustManager implements X509TrustManager {

        static MyX509TrustManager manger = new MyX509TrustManager();

        public MyX509TrustManager() {
        }

        /**
         * @return java.security.cert.X509Certificate[]
         * @Method: getAcceptedIssuers
         * @Decription: TODO
         * @author mazhao
         * @Date 2018/11/28 16:44
         * @Param []
         * @throw
         */
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        /**
         * @Method: checkClientTrusted
         * @Decription: TODO
         * @author mazhao
         * @Date 2018/11/28 16:32
         * @Param [chain, authType]
         * @throw
         */
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        /**
         * @Method: checkServerTrusted
         * @Decription: TODO
         * @author mazhao
         * @Date 2018/11/28 16:32
         * @Param [chain, authType]
         * @throw
         */
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }
    }

    /**
     * 解决由于服务器证书问题导致HTTPS无法访问的情况 PS:HTTPS hostname wrong: should be <localhost>
     */
    public static class TrustAnyHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            //直接返回true
            return true;
        }
    }
}
